package com.qq.server.tools;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    private StringUtils() {
    }

    public static boolean isNotEmpty(String id) {
        return !isEmptyString(id);
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean isEmptyString(String s) {
        return s == null || "".equals(s);
    }

    public static String getMathRandomCode() {
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }
        }
        return fourRandom;
    }

    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,7,5,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    public static String formatStr(String str, String separator) {
        StringBuffer sb = new StringBuffer("");
        if (str.lastIndexOf(separator) < 0) {
            return str;
        } else {
            String[] split = str.split(separator);
            sb.append(split[0]);
            for (int i = 1; i < split.length; i++) {
                char[] chars = split[1].toCharArray();
                chars[0] = Character.toUpperCase(chars[0]);
                sb.append(new String(chars));
            }
        }
        return sb.toString();
    }
}
